package edu.yngsxy.wkstudy.core.ai.service;

import cn.hutool.ai.AIServiceFactory;
import cn.hutool.ai.ModelName;
import cn.hutool.ai.core.AIConfigBuilder;
import cn.hutool.ai.core.Message;
import cn.hutool.ai.model.doubao.DoubaoService;
import edu.yngsxy.wkstudy.core.ai.service.MultiTurnChatMessage;
import edu.yngsxy.wkstudy.core.ai.service.WkDoubaoService;
import edu.yngsxy.wkstudy.core.model.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * AI大模型-豆包
 */
@RestController
@RequestMapping("/api/v1/ai/doubao")
@Tag(name = "AI大模型") // 分组标签
@Slf4j
public class DoubaoController {
    @Value("${ai.doubao.apikey}")
    private String apikey;
    @Value("${ai.doubao.model}")
    private String model;
    DoubaoService doubaoService;
    @Autowired
    WkDoubaoService wkDoubaoService;
    @PostConstruct
    public void init() {// 初始化豆包服务
        doubaoService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.DOUBAO.getValue()).setApiKey(apikey).setModel(model).build(), DoubaoService.class);
    }


    @PostMapping("/chat")
    @Operation(summary = "豆包-单论对话", description = "单论对话")
    public Result<String> chat(
            @Parameter(description = "发送消息", required = true)
            @RequestBody MultiTurnChatMessage chatMessage) {
        String answer= wkDoubaoService.chat(chatMessage.getMsg());
        return Result.success(answer);
    }

    @PostMapping("/multi-turn-chat")
    @Operation(summary = "豆包-多论对话", description = "多论对话")
    public Result<Map<String, Object>> multiTurnChat(
            @Parameter(description = "发送消息", required = true)
            @RequestBody MultiTurnChatMessage chatMessage) {
        Map<String, Object> answer = wkDoubaoService.chat( chatMessage);
        return Result.success(answer);
    }
}
